const mysql = require('mysql')

const db = mysql.createConnection({
  host: '127.0.0.1',
  user: 'root',
  password: '123456',
  port: 3306,
  database: 'traffic',
  timezone: 'SYSTEM'
})
db.connect()
//获取所有文章
function getAll(sql) {
  return new Promise((resolve, reject) => {
    db.query(sql, (err, data) => {
      if (err) reject(err)

      resolve(data)
    })
  }).catch(()=>{})
}
function getById(sql) {
  return new Promise((resolve, reject) => {
    db.query(sql, (err, data) => {
      if (err) reject(err)
      data.length != 0 ? resolve(data[0]) : resolve(null)
    })
  }).catch(()=>{})
}
function exec(sql) {
  return new Promise((resolve, reject) => {
    db.query(sql, (err, data) => {
      if (err) reject(err)

      resolve(data)
    })
  }).catch(()=>{})
}
module.exports = {
  getAll,
  getById,
  exec
}
